<?php


namespace app\system\service;

class VersionService
{
	public static function getlist($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["type"]) && $options["type"] != "") {
			$where[] = ["type", "=", $options["type"]];
		}
		if (isset($options["number"]) && $options["number"] != "") {
			$where[] = ["number", "like", "%" . $options["number"] . "%"];
		}
		$list = \think\Db::name("app_version")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_version")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_array_info($item)
	{
		$item["scriptEditConfig"] = json_decode($item["scriptEditConfig"], true);
		$item["scriptEditConfig"] = ["mode" => $item["scriptEditConfig"]["mode"] ?? "default", "coder_language" => $item["scriptEditConfig"]["coder_language"] ?? ""];
		return $item;
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("app_version")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_array_info($item)];
	}
	public static function add($options)
	{
		$isSystemAuthUnpaid = isSystemAuthUnpaid();
		if ($isSystemAuthUnpaid && \think\Db::name("app_version")->where(["appid" => $options["appid"]])->count() >= \app\common\model\FreeQuota::AppVersionQuota) {
			return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,一个应用最多只能添加" . \app\common\model\FreeQuota::AppVersionQuota . "个版本" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
		}
		\think\Db::startTrans();
		try {
			\think\Db::name("app_version")->insert(["appid" => $options["appid"], "type" => $options["type"], "number" => $options["number"], "content" => $options["content"], "update_package" => $options["update_package"], "complete_package" => $options["complete_package"], "force" => $options["force"], "exec_script" => empty($options["exec_script"]) ? "" : $options["exec_script"], "time" => time()]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_version")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$data = ["type" => $options["type"], "number" => $options["number"], "content" => $options["content"], "update_package" => $options["update_package"], "complete_package" => $options["complete_package"], "force" => $options["force"], "exec_script" => empty($options["exec_script"]) ? $row["exec_script"] : $options["exec_script"]];
			if (!empty($options["update_time"])) {
				$data["time"] = time();
			}
			\think\Db::name("app_version")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_version")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_version")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}